#!/usr/bin/env python
# encoding: utf-8
'''
@author: wangjianrong
@software: pycharm
@file: read_obstructions.py
@time: 2020/9/27 15:28
@desc:从静态图片中抠障碍物
'''

from glob import glob
import cv2
import json
import numpy as np
import matplotlib.pyplot as plt
import os

root_folder = '/workspace_wjr/shm/dataset/road_obstructions/'
list_imgs = glob(root_folder + '*.jpg')

for img_path in list_imgs:
    label_path = img_path[:-3]+'json'
    if not os.path.exists(label_path):
        continue
    label = json.load(open(label_path,'r'))
    img = cv2.imread(img_path)
    h,w,c = img.shape
    mask = np.zeros((h,w),dtype=np.uint8)
    shapes = label['shapes']
    for shape in shapes:
        contour = np.array(shape['points']).astype(int)
        contour = contour[:,None,:]
        cv2.fillConvexPoly(mask,contour,255)
    img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
    target = np.zeros_like(img)
    target[mask==255] = img[mask==255]
    plt.imshow(target)
    plt.show()
    plt.pause(10)



